package com.hzx.demo.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.hzx.demo.R;
import com.hzx.demo.util.Entry;
import com.hzx.demo.util.HZXLog;

import java.io.IOException;

/**
 * Created by ldkxingzhe on 2016/5/17.
 */
@Entry(desc = "一个加载大图片的测试, 无损加载", createTime = "2016-05-17 11:31")
public class HugeImageViewActivity extends AppCompatActivity{
    @SuppressWarnings("unused")
    private static final String TAG = "HugeImageViewActivity";

    private ImageView mImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huge_image_view);
        mImageView = (ImageView) findViewById(R.id.iv_image);
        loadBitmap();
    }

    private void loadBitmap() {
        HZXLog.v(TAG, "loadBitmap");
        new AsyncTask<Void, Void, Bitmap>(){
            @Override
            protected Bitmap doInBackground(Void... params) {
                try {
                    BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(getResources().openRawResource(R.raw.china_map), false);
                    Rect rect = new Rect(800, 800, 1600, 1600);
                    return decoder.decodeRegion(rect, null);
                } catch (IOException e) {
                    HZXLog.e(TAG, e.getMessage(), e);
                    return null;
                }
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if(bitmap == null){
                    HZXLog.v(TAG, "onPostExecute, and bitmap is null");
                    return;
                }

                HZXLog.v(TAG, "onPostExecute, and bitmap.width is %d, height is %d.",
                        bitmap.getWidth(), bitmap.getHeight());
                mImageView.setImageBitmap(bitmap);
            }
        }.execute();
    }
}
